#include "task.h"

uint16_t gUpCount = 0;
uint8_t gUpFlag=0;

float gAngle[2];

void DetectAction(void)
{
    static uint8_t step=0, start_tick=0, error_tick=0;
    int16_t gyro[3];
    GetGyro(gyro);

    GetAngles(gAngle);

    switch (step)
    {
    case 0:
        if(gyro[1] < 1000)
        {
            step =1;
            start_tick = GetSysTick();
        }
        break;
    case 1:
        if(gAngle[1] < 20)
        {
            step =2;
            gUpFlag = 1;
            gUpCount++;
            //temp test
            static uint8_t count;
            printf("up->%d\r\n", count++);
            SetBeepFun(0, BeepOnMement, 200);
        }
        else
        {
            if((GetSysTick()-start_tick) > 500)
            {
                step = 0;
            }
        }
        break;
    case 2:
        if(gAngle[1] > 50)
        {
            step =0;
        }
        break;

    default:
        break;
    }
}

void OLED_ShowLoop(uint16_t Cycle)
{
    static uint32_t TimeTick;
    if((GetSysTick()-TimeTick) > Cycle)
    {
        TimeTick = GetSysTick();

        RTC_DateTypeDef RTC_Date;
        RTC_TimeTypeDef RTC_Time;
        HAL_RTC_GetTime(&hrtc, &RTC_Time, RTC_FORMAT_BIN);
        HAL_RTC_GetDate(&hrtc, &RTC_Date, RTC_FORMAT_BIN);

        uint8_t buf[10];

        sprintf(buf, "angle->%.1f", -gAngle[1]);  
        OLED_ShowString(5,5,buf, 16, 1);  

        sprintf(buf, "count->%d",  gUpCount);  
        OLED_ShowString(5,30,buf, 16, 1);  

        sprintf(buf, "%02d:%02d:%02d",  RTC_Time.Hours, RTC_Time.Minutes, RTC_Time.Seconds);
        OLED_ShowString(5,50,buf, 16, 1);
   
        OLED_Refresh(); 
    }  
}

void TaskLoop(uint16_t Cycle)
{
    static uint32_t TimeTick;
    if((GetSysTick()-TimeTick) > Cycle)
    {
        TimeTick = GetSysTick();
        DetectAction();
    }
}



